/*
 * Created on 2005/11/02
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package net.sf.rose.file.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

/**
 * @author fengjian
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class FileUtils {

	/**
	 * 
	 */
	public static boolean exists(File file) {
		return file.exists() ? true : false;
	}

	/**
	 * Create Directory
	 */
	public static void creatDirectory(File file) {
		if (!exists(file)) {
			file.mkdirs();
		}
	}

	/**
	 * Create Directory
	 */
	public static void creatDirectory(String strPath) {
		File file = new File(strPath);
		creatDirectory(file);
	}

	/**
	 * @throws IOException
	 * 
	 */
	public static void creatFile(File file) throws IOException {
		if (!exists(file)) {
			file.createNewFile();
		}
	}

	/**
	 * @throws IOException
	 * 
	 */
	public static void creatFile(String strFileName) throws IOException {
		File file = new File(strFileName);
		creatFile(file);
	}

	/**
	 * 
	 */
	public static void deleteFile(File file) {
		if (file.isFile()) {
			file.delete();
		}
	}

	/**
	 * 
	 */
	public static void deleteFile(String strFileName) {
		File file = new File(strFileName);
		deleteFile(file);
	}

	/**
	 * 
	 */
	private static void deleteAllOfDirectory(File file) {

		File[] files = file.listFiles();
		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				deleteAllOfDirectory(files[i]);
			}
		}
		file.delete();
	}

	/**
	 * 
	 */
	public static void deleteDirectory(File file) {
		if (file.isDirectory()) {
			deleteAllOfDirectory(file);
		}
	}

	/**
	 * 
	 */
	public static void deleteDirectory(String strPath) {
		File file = new File(strPath);
		deleteDirectory(file);
	}

	/**
	 * ctrate InputStream by String.
	 */
	public static InputStream toInputStream(String string) {
		return new ByteArrayInputStream(string.getBytes());
	}

	/**
	 * Copies in Stream to out Stream.
	 */
	public static void copy(InputStream in, OutputStream out)
			throws IOException {
		// Transfer bytes from in to out
		byte[] buf = new byte[1024];
		int len;
		while ((len = in.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		in.close();
		out.close();
	}

	/**
	 * Copies src file to dst file. If the dst file does not exist, it is
	 * created.
	 */
	public static void copy(File src, File dst) throws IOException {
		InputStream in = new FileInputStream(src);
		OutputStream out = new FileOutputStream(dst);
		copy(in, out);
	}

	/**
	 * 
	 */
	public static void copy(InputStream in, File dst) throws IOException {
		OutputStream out = new FileOutputStream(dst);
		copy(in, out);
	}

	/**
	 * 
	 */
	public static void copy(File src, OutputStream out) throws IOException {
		InputStream in = new FileInputStream(src);
		copy(in, out);
	}

	/**
	 * 
	 */
	public static Object copy(Object object) throws Exception {
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		ObjectOutput out = new ObjectOutputStream(bout);
		out.writeObject(object);

		ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
		ObjectInputStream in = new ObjectInputStream(bin);
		object = in.readObject();

		bout.close();
		bin.close();
		in.close();
		out.close();
		return object;
	}

	/**
	 * @throws Exception
	 * 
	 */
	public static void main(String args[]) throws Exception {
		copy(new File("C:/Documents and Settings/fengj/My Documents/src/modal"),new File("d:/"));
	}

}
